/**
 * 
 */
package org.t2.backstage.module.player.service;

import java.util.List;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.service.service.AbstractService;
import org.springframework.stereotype.Service;
import org.t2.backstage.module.player.model.Player;

/**
 * @author YY
 *
 */
@Service
public class PlayerService extends AbstractService<Player> {

	
	public Player getEntity(long id)  {
		return this.repository().getByKeys(id);
	}
	
	public List<Player> getByOpenId(long channelId, String openId) {
		QuerySet querySet = new QuerySet();
		querySet.addCondition("channel_id = ?", channelId);
		querySet.addCondition("open_id = ?", openId);
		querySet.formWhere();
		return repository().getBaseDao().queryListWhere(querySet.getWhere(), querySet.getParams());
	}
	
	public PageData<Player> query(QuerySet querySet) {
		List<Player> list = this.repository().getBaseDao().queryListWhere(querySet.getWhere(), querySet.getParams());
		
		PageData<Player> pageData = new PageData<>();
		pageData.setCount(this.repository().getBaseDao().countWhere(querySet.getCountWhere(), querySet.getCountParams()));
		pageData.setData(list);
		return pageData;
	}


	public void insertUpdate(List<Player> players) {
		this.repository().getBaseDao().insertUpdate(players);
	}
	
	/**
	 * 是否为老玩家
	 * @param openId
	 * @param serverId
	 * @return
	 */
	public boolean isOld(String openId, int serverId) {
		QuerySet querySet = new QuerySet();
		querySet.addCondition("open_id = ?", openId);
		querySet.addCondition("server_id < ?", serverId);
		querySet.formWhere();
		List<Player> list = this.repository().getBaseDao().queryListWhere(querySet.getWhere(), querySet.getParams());
		return !list.isEmpty();
	}
//	
//	public long queryCount(QuerySet querySet) {
//		return this.repository().getBaseDao().countWhere(querySet.getCountWhere(), querySet.getCountParams());
//	}

	@Override
	public void mergeServer(int mergeDbZoneId) {
		//不必理会
	}
}
